package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.service.AddressBookService;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrderService;
import com.itheima.reggie.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private UserService userService;

    @Autowired
    private AddressBookService addressBookService;


    /**
     * 用户下单
     *
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders) {
        log.info("订单数据：{}", orders);
        orderService.submit(orders);
        return R.success("下单成功");
    }

    /**
     * 移动端订单明细
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R<Page> userPage(int page, int pageSize) {

        // 构造分页构造器对象
        Page<Orders> pageInfo = new Page<>(page, pageSize);
        Page<OrdersDto> ordersDtoPage = new Page<>();

        // 条件构造器
        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();

        // 添加排序条件 根据创建时间进行排序
        lqw.orderByDesc(Orders::getOrderTime);
        //设置用户id，指定当前是哪个用户的购物车数据
        lqw.eq(Orders::getUserId, BaseContext.getCurrentId());

        // 分页查询
        orderService.page(pageInfo, lqw);

        // 对象拷贝
        BeanUtils.copyProperties(pageInfo, ordersDtoPage, "records");

        // 对OrderDto查询对应菜品/套餐
        List<Orders> records = pageInfo.getRecords();
        List<OrdersDto> list = records.stream().map((item) -> {
            OrdersDto ordersDto = new OrdersDto();
            //获取订单Id
            Long orderId = item.getId();
            //为orderDetails属性赋值
            LambdaQueryWrapper<OrderDetail> wrapper = new LambdaQueryWrapper<>();
            //根据订单Id查询对应的订单明细
            wrapper.eq(OrderDetail::getOrderId, orderId);
            List<OrderDetail> orderDetails = orderDetailService.list(wrapper);
            BeanUtils.copyProperties(item, ordersDto);
            //对orderDto的orderDetails属性进行赋值
            ordersDto.setOrderDetails(orderDetails);

            return ordersDto;
        }).collect(Collectors.toList());

        ordersDtoPage.setRecords(list);

        return R.success(ordersDtoPage);
    }

    /**
     * 管理端订单明细
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> Page(int page, int pageSize, String number, String beginTime, String endTime) {
        // 构造分页构造器对象
        Page<Orders> pageInfo = new Page<>(page, pageSize);
        // Page<OrdersDto> ordersDtoPage = new Page<>();

        //创建条件构造器
        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();

        //添加条件,根据number进行模糊查询
        lqw.like(number != null, Orders::getNumber, number);
        lqw.gt(StringUtils.isNotEmpty(beginTime), Orders::getOrderTime, beginTime);
        lqw.lt(StringUtils.isNotEmpty(endTime), Orders::getOrderTime, endTime);

        //添加排序条件(根据更新时间降序排序)
        lqw.orderByDesc(Orders::getOrderTime);

        // 分页查询
        orderService.page(pageInfo, lqw);

        return R.success(pageInfo);
    }

    @PutMapping
    public R<String> update(@RequestBody Orders orders) {
        log.info("id:{}派送状态为：{}", orders.getId(), orders.getStatus());

        orderService.updateById(orders);
        return R.success("修改成功");
    }
}